//
//  HJSuperstarSoccerView.m
//  Keping
//
//  Created by Joanlove on 2017/12/30.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "HJSuperstarSoccerView.h"
#import "HJSuperstarSoccerTimeCell.h"

@interface HJSuperstarSoccerView()

@end

@implementation HJSuperstarSoccerView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView registerNib:[UINib nibWithNibName:[HJSuperstarSoccerTimeCell className] bundle:nil] forCellReuseIdentifier:[HJSuperstarSoccerTimeCell className]];
    }
    return self;
}


- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
    }
    return self;
}

- (NSMutableArray<KShopItemTimeView *> *)selectShopItemTimeViews {
    if (!_selectShopItemTimeViews) {
        _selectShopItemTimeViews = [NSMutableArray new];
    }
    return _selectShopItemTimeViews;
}

- (void)setShopItemTimeViews:(NSArray<KShopItemTimeView *> *)shopItemTimeViews {
    _shopItemTimeViews = shopItemTimeViews;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:[HJSuperstarSoccerTimeCell className] bundle:nil] forCellReuseIdentifier:[HJSuperstarSoccerTimeCell className]];
    [self.tableView reloadData];
}

#pragma mark - UITableView Delegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.shopItemTimeViews.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KShopItemTimeView *items = [self.shopItemTimeViews objectAtIndex:indexPath.row];
    HJSuperstarSoccerTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:[HJSuperstarSoccerTimeCell className]];
    cell.timeLB.text = [NSString stringWithFormat:@"%@ -- %@",items.businessHoursStart,items.businessHoursEnd];
    [cell.timeBtn setSelected:items.select];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KShopItemTimeView *items = [self.shopItemTimeViews objectAtIndex:indexPath.row];
    items.select = !items.select;
    if (items.select) {
        [self.selectShopItemTimeViews addObject:items];
    } else {
        [self.selectShopItemTimeViews removeObject:items];
    }
    [tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationNone];
}

- (IBAction)actionForOK:(id)sender {
    if (self.superstarSoccerHidden) {
        self.superstarSoccerHidden(self.selectShopItemTimeViews);
    }
}

- (IBAction)actionForCannel:(id)sender {
    if (self.superstarSoccerHidden) {
        self.superstarSoccerHidden(nil);
    }
}

@end
